package com.zha.util;

import com.zha.constant.HttpConstants;
import jakarta.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

/**
 * @Title: 只有光头才能变强
 * @Author: 炸炸
 * @Date: 2024/12/10 15:18\
 * 响应工具类
 */
public class ResponseUtils {
    public static void write(HttpServletResponse response, String jsonStr){
        // 设置响应头
        response.setContentType(HttpConstants.APPLICATION_JSON_UTF_8_VALUE);
        response.setCharacterEncoding(HttpConstants.UTF_8);
        // 获取writer对象
        PrintWriter writer = null ;
        try {
            writer = response.getWriter();
            writer.write(jsonStr);
            writer.flush();
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            if (writer!= null){
                writer.close();
            }
        }
    }
}
